<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>perldata - Perl data types - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/perldata">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perldata.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/perldata">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/perldata">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/perldata">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/perldata">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/perldata">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/perldata">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/perldata">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/perldata">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/perldata">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/perldata">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/perldata">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/perldata">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/perldata">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/perldata">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/perldata">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/perldata">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/perldata">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/perldata">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/perldata">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/perldata">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/perldata">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/perldata">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/perldata">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/perldata">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/perldata">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/perldata">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/perldata">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/perldata">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/perldata">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/perldata">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/perldata">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/perldata">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/perldata">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/perldata">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/perldata">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/perldata">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/perldata">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/perldata">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/perldata">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/perldata">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/perldata">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/perldata">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/perldata">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/perldata">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/perldata">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/perldata">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/perldata">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/perldata">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/perldata">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/perldata">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/perldata">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/perldata">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/perldata">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/perldata">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/perldata">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/perldata">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/perldata">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/perldata">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/perldata">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/perldata">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/perldata">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/perldata">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/perldata">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/perldata">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/perldata">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/perldata">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/perldata">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/perldata">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/perldata">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/perldata">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/perldata">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/perldata">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/perldata">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/perldata">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/perldata">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/perldata">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/perldata">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/perldata">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/perldata">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/perldata">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perldata.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/perldata">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/perldata">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/perldata">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/perldata">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/perldata">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/perldata">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/perldata">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/perldata">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/perldata">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/perldata">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/perldata">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/perldata">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/perldata">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/perldata">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/perldata">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/perldata">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/perldata">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/perldata">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/perldata">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/perldata">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/perldata">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/perldata">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/perldata">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/perldata">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/perldata">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/perldata">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/perldata">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/perldata">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/perldata">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/perldata">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/perldata">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/perldata">5.35.1</a>
          <a class="dropdown-item active" href="perldata.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perldata.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="perl.html">Perl</a>
          <a class="dropdown-item" href="perlintro.html">Intro</a>
          <a class="dropdown-item" href="perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlop.html">Operators</a>
          <a class="dropdown-item" href="functions.html">Functions</a>
          <a class="dropdown-item" href="variables.html">Variables</a>
          <a class="dropdown-item" href="modules.html">Modules</a>
          <a class="dropdown-item" href="perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlcommunity.html">Community</a>
          <a class="dropdown-item" href="perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="perldata.html">perldata</a>
          <div id="more">
            (<a href="perldata.txt">source</a>,
            <a href="https://metacpan.org/pod/perldata">CPAN</a>)
          </div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <h1><a id="toc">CONTENTS</a></h1>
                  <ul>
              <li>
                <a class="text-decoration-none" href="perldata.html#NAME">NAME</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perldata.html#DESCRIPTION">DESCRIPTION</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perldata.html#Variable-names">Variable names    </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perldata.html#Identifier-parsing">Identifier parsing </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perldata.html#Context">Context   </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perldata.html#Scalar-values">Scalar values    </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perldata.html#Scalar-value-constructors">Scalar value constructors  </a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perldata.html#Special-floating-point:-infinity-(Inf)-and-not-a-number-(NaN)">Special floating point: infinity (Inf) and not-a-number (NaN)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perldata.html#Version-Strings">Version Strings   </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perldata.html#Special-Literals">Special Literals         </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perldata.html#Barewords">Barewords </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perldata.html#Array-Interpolation">Array Interpolation   </a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perldata.html#List-value-constructors">List value constructors </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perldata.html#Subscripts">Subscripts</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perldata.html#Multi-dimensional-array-emulation">Multi-dimensional array emulation</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perldata.html#Slices">Slices   </a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perldata.html#Key/Value-Hash-Slices">Key/Value Hash Slices</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perldata.html#Index/Value-Array-Slices">Index/Value Array Slices</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perldata.html#Typeglobs-and-Filehandles">Typeglobs and Filehandles   </a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perldata.html#SEE-ALSO">SEE ALSO</a>
              </li>
          </ul>

      <h1 id="NAME"><a class="permalink" href="perldata.html#NAME">#</a>NAME</h1>

<p>perldata - Perl data types</p>

<h1 id="DESCRIPTION"><a class="permalink" href="perldata.html#DESCRIPTION">#</a>DESCRIPTION</h1>

<h2 id="Variable-names"><a class="permalink" href="perldata.html#Variable-names">#</a><a id="Variable"></a>Variable names    </h2>

<p>Perl has three built-in data types: scalars, arrays of scalars, and associative arrays of scalars, known as &quot;hashes&quot;. A scalar is a single string (of any size, limited only by the available memory), number, or a reference to something (which will be discussed in <a href="perlref.html">perlref</a>). Normal arrays are ordered lists of scalars indexed by number, starting with 0. Hashes are unordered collections of scalar values indexed by their associated string key.</p>

<p>Values are usually referred to by name, or through a named reference. The first character of the name tells you to what sort of data structure it refers. The rest of the name tells you the particular value to which it refers. Usually this name is a single <i>identifier</i>, that is, a string beginning with a letter or underscore, and containing letters, underscores, and digits. In some cases, it may be a chain of identifiers, separated by <code>::</code> (or by the slightly archaic <code>&#39;</code>); all but the last are interpreted as names of packages, to locate the namespace in which to look up the final identifier (see <a href="perlmod.html#Packages">&quot;Packages&quot; in perlmod</a> for details). For a more in-depth discussion on identifiers, see <a href="perldata.html#Identifier-parsing">&quot;Identifier parsing&quot;</a>. It&#39;s possible to substitute for a simple identifier, an expression that produces a reference to the value at runtime. This is described in more detail below and in <a href="perlref.html">perlref</a>. </p>

<p>Perl also has its own built-in variables whose names don&#39;t follow these rules. They have strange names so they don&#39;t accidentally collide with one of your normal variables. Strings that match parenthesized parts of a regular expression are saved under names containing only digits after the <code>$</code> (see <a href="perlop.html">perlop</a> and <a href="perlre.html">perlre</a>). In addition, several special variables that provide windows into the inner working of Perl have names containing punctuation characters. These are documented in <a href="perlvar.html">perlvar</a>. </p>

<p>Scalar values are always named with &#39;$&#39;, even when referring to a scalar that is part of an array or a hash. The &#39;$&#39; symbol works semantically like the English word &quot;the&quot; in that it indicates a single value is expected. </p>

<pre><code>$days		# the simple scalar value &quot;days&quot;
$days[28]		# the 29th element of array @days
$days{&#39;Feb&#39;}	# the &#39;Feb&#39; value from hash %days
$#days		# the last index of array @days</code></pre>

<p>Entire arrays (and slices of arrays and hashes) are denoted by &#39;@&#39;, which works much as the word &quot;these&quot; or &quot;those&quot; does in English, in that it indicates multiple values are expected. </p>

<pre><code>@days		# ($days[0], $days[1],... $days[n])
@days[3,4,5]	# same as ($days[3],$days[4],$days[5])
@days{&#39;a&#39;,&#39;c&#39;}	# same as ($days{&#39;a&#39;},$days{&#39;c&#39;})</code></pre>

<p>Entire hashes are denoted by &#39;%&#39;: </p>

<pre><code>%days		# (key1, val1, key2, val2 ...)</code></pre>

<p>In addition, subroutines are named with an initial &#39;&amp;&#39;, though this is optional when unambiguous, just as the word &quot;do&quot; is often redundant in English. Symbol table entries can be named with an initial &#39;*&#39;, but you don&#39;t really care about that yet (if ever :-).</p>

<p>Every variable type has its own namespace, as do several non-variable identifiers. This means that you can, without fear of conflict, use the same name for a scalar variable, an array, or a hash--or, for that matter, for a filehandle, a directory handle, a subroutine name, a format name, or a label. This means that $foo and @foo are two different variables. It also means that <code>$foo[1]</code> is a part of @foo, not a part of $foo. This may seem a bit weird, but that&#39;s okay, because it is weird. </p>

<p>Because variable references always start with &#39;$&#39;, &#39;@&#39;, or &#39;%&#39;, the &quot;reserved&quot; words aren&#39;t in fact reserved with respect to variable names. They <i>are</i> reserved with respect to labels and filehandles, however, which don&#39;t have an initial special character. You can&#39;t have a filehandle named &quot;log&quot;, for instance. Hint: you could say <code>open(LOG,&#39;logfile&#39;)</code> rather than <code>open(log,&#39;logfile&#39;)</code>. Using uppercase filehandles also improves readability and protects you from conflict with future reserved words. Case <i>is</i> significant--&quot;FOO&quot;, &quot;Foo&quot;, and &quot;foo&quot; are all different names. Names that start with a letter or underscore may also contain digits and underscores.  </p>

<p>It is possible to replace such an alphanumeric name with an expression that returns a reference to the appropriate type. For a description of this, see <a href="perlref.html">perlref</a>.</p>

<p>Names that start with a digit may contain only more digits. Names that do not start with a letter, underscore, digit or a caret are limited to one character, e.g., <code>$%</code> or <code>$$</code>. (Most of these one character names have a predefined significance to Perl. For instance, <code>$$</code> is the current process id. And all such names are reserved for Perl&#39;s possible use.)</p>

<h2 id="Identifier-parsing"><a class="permalink" href="perldata.html#Identifier-parsing">#</a><a id="Identifier"></a>Identifier parsing </h2>

<p>Up until Perl 5.18, the actual rules of what a valid identifier was were a bit fuzzy. However, in general, anything defined here should work on previous versions of Perl, while the opposite -- edge cases that work in previous versions, but aren&#39;t defined here -- probably won&#39;t work on newer versions. As an important side note, please note that the following only applies to bareword identifiers as found in Perl source code, not identifiers introduced through symbolic references, which have much fewer restrictions. If working under the effect of the <code>use utf8;</code> pragma, the following rules apply:</p>

<pre><code class="plaintext">/ (?[ ( \p{Word} &amp; \p{XID_Start} ) + [_] ])
  (?[ ( \p{Word} &amp; \p{XID_Continue} ) ]) *    /x</code></pre>

<p>That is, a &quot;start&quot; character followed by any number of &quot;continue&quot; characters. Perl requires every character in an identifier to also match <code>\w</code> (this prevents some problematic cases); and Perl additionally accepts identifier names beginning with an underscore.</p>

<p>If not under <code>use utf8</code>, the source is treated as ASCII + 128 extra generic characters, and identifiers should match</p>

<pre><code class="plaintext">/ (?aa) (?!\d) \w+ /x</code></pre>

<p>That is, any word character in the ASCII range, as long as the first character is not a digit.</p>

<p>There are two package separators in Perl: A double colon (<code>::</code>) and a single quote (<code>&#39;</code>). Normal identifiers can start or end with a double colon, and can contain several parts delimited by double colons. Single quotes have similar rules, but with the exception that they are not legal at the end of an identifier: That is, <code>$&#39;foo</code> and <code>$foo&#39;bar</code> are legal, but <code>$foo&#39;bar&#39;</code> is not.</p>

<p>Additionally, if the identifier is preceded by a sigil -- that is, if the identifier is part of a variable name -- it may optionally be enclosed in braces.</p>

<p>While you can mix double colons with singles quotes, the quotes must come after the colons: <code>$::::&#39;foo</code> and <code>$foo::&#39;bar</code> are legal, but <code>$::&#39;::foo</code> and <code>$foo&#39;::bar</code> are not.</p>

<p>Put together, a grammar to match a basic identifier becomes</p>

<pre><code>/
 (?(DEFINE)
     (?&lt;variable&gt;
         (?&amp;sigil)
         (?:
                 (?&amp;normal_identifier)
             |   \{ \s* (?&amp;normal_identifier) \s* \}
         )
     )
     (?&lt;normal_identifier&gt;
         (?: :: )* &#39;?
          (?&amp;basic_identifier)
          (?: (?= (?: :: )+ &#39;? | (?: :: )* &#39; ) (?&amp;normal_identifier) )?
         (?: :: )*
     )
     (?&lt;basic_identifier&gt;
       # is use utf8 on?
         (?(?{ (caller(0))[8] &amp; $utf8::hint_bits })
             (?&amp;Perl_XIDS) (?&amp;Perl_XIDC)*
           | (?aa) (?!\d) \w+
         )
     )
     (?&lt;sigil&gt; [&amp;*\$\@\%])
     (?&lt;Perl_XIDS&gt; (?[ ( \p{Word} &amp; \p{XID_Start} ) + [_] ]) )
     (?&lt;Perl_XIDC&gt; (?[ \p{Word} &amp; \p{XID_Continue} ]) )
 )
/x</code></pre>

<p>Meanwhile, special identifiers don&#39;t follow the above rules; For the most part, all of the identifiers in this category have a special meaning given by Perl. Because they have special parsing rules, these generally can&#39;t be fully-qualified. They come in six forms (but don&#39;t use forms 5 and 6):</p>

<ol>

<li><p>A sigil, followed solely by digits matching <code>\p{POSIX_Digit}</code>, like <code>$0</code>, <code>$1</code>, or <code>$10000</code>.</p>

</li>
<li><p>A sigil followed by a single character matching the <code>\p{POSIX_Punct}</code> property, like <code>$!</code> or <code>%+</code>, except the character <code>&quot;{&quot;</code> doesn&#39;t work.</p>

</li>
<li><p>A sigil, followed by a caret and any one of the characters <code>[][A-Z^_?\]</code>, like <code>$^V</code> or <code>$^]</code>.</p>

</li>
<li><p>Similar to the above, a sigil, followed by bareword text in braces, where the first character is a caret. The next character is any one of the characters <code>[][A-Z^_?\]</code>, followed by ASCII word characters. An example is <code>${^GLOBAL_PHASE}</code>.</p>

</li>
<li><p>A sigil, followed by any single character in the range <code>[\xA1-\xAC\xAE-\xFF]</code> when not under <code><span style="white-space: nowrap;">&quot;use utf8&quot;</span></code>. (Under <code><span style="white-space: nowrap;">&quot;use utf8&quot;</span></code>, the normal identifier rules given earlier in this section apply.) Use of non-graphic characters (the C1 controls, the NO-BREAK SPACE, and the SOFT HYPHEN) has been disallowed since v5.26.0. The use of the other characters is unwise, as these are all reserved to have special meaning to Perl, and none of them currently do have special meaning, though this could change without notice.</p>

<p>Note that an implication of this form is that there are identifiers only legal under <code><span style="white-space: nowrap;">&quot;use utf8&quot;</span></code>, and vice-versa, for example the identifier <code>$état</code> is legal under <code><span style="white-space: nowrap;">&quot;use utf8&quot;</span></code>, but is otherwise considered to be the single character variable <code>$é</code> followed by the bareword <code>&quot;tat&quot;</code>, the combination of which is a syntax error.</p>

</li>
<li><p>This is a combination of the previous two forms. It is valid only when not under <span style="white-space: nowrap;"><code>&quot;use utf8&quot;</code></span> (normal identifier rules apply when under <span style="white-space: nowrap;"><code>&quot;use utf8&quot;</code></span>). The form is a sigil, followed by text in braces, where the first character is any one of the characters in the range <code>[\x80-\xFF]</code> followed by ASCII word characters up to the trailing brace.</p>

<p>The same caveats as the previous form apply: The non-graphic characters are no longer allowed with <span style="white-space: nowrap;">&quot;use utf8&quot;</span>, it is unwise to use this form at all, and utf8ness makes a big difference.</p>

</li>
</ol>

<p>Prior to Perl v5.24, non-graphical ASCII control characters were also allowed in some situations; this had been deprecated since v5.20.</p>

<h2 id="Context"><a class="permalink" href="perldata.html#Context">#</a>Context   </h2>

<p>The interpretation of operations and values in Perl sometimes depends on the requirements of the context around the operation or value. There are two major contexts: list and scalar. Certain operations return list values in contexts wanting a list, and scalar values otherwise. If this is true of an operation it will be mentioned in the documentation for that operation. In other words, Perl overloads certain operations based on whether the expected return value is singular or plural. Some words in English work this way, like &quot;fish&quot; and &quot;sheep&quot;.</p>

<p>In a reciprocal fashion, an operation provides either a scalar or a list context to each of its arguments. For example, if you say</p>

<pre><code class="plaintext">int( &lt;STDIN&gt; )</code></pre>

<p>the integer operation provides scalar context for the &lt;&gt; operator, which responds by reading one line from STDIN and passing it back to the integer operation, which will then find the integer value of that line and return that. If, on the other hand, you say</p>

<pre><code class="plaintext">sort( &lt;STDIN&gt; )</code></pre>

<p>then the sort operation provides list context for &lt;&gt;, which will proceed to read every line available up to the end of file, and pass that list of lines back to the sort routine, which will then sort those lines and return them as a list to whatever the context of the sort was.</p>

<p>Assignment is a little bit special in that it uses its left argument to determine the context for the right argument. Assignment to a scalar evaluates the right-hand side in scalar context, while assignment to an array or hash evaluates the righthand side in list context. Assignment to a list (or slice, which is just a list anyway) also evaluates the right-hand side in list context.</p>

<p>When you use the <code>use warnings</code> pragma or Perl&#39;s <b>-w</b> command-line option, you may see warnings about useless uses of constants or functions in &quot;void context&quot;. Void context just means the value has been discarded, such as a statement containing only <code>&quot;fred&quot;;</code> or <code>getpwuid(0);</code>. It still counts as scalar context for functions that care whether or not they&#39;re being called in list context.</p>

<p>User-defined subroutines may choose to care whether they are being called in a void, scalar, or list context. Most subroutines do not need to bother, though. That&#39;s because both scalars and lists are automatically interpolated into lists. See <a href="perlfunc.html#wantarray">&quot;wantarray&quot; in perlfunc</a> for how you would dynamically discern your function&#39;s calling context.</p>

<h2 id="Scalar-values"><a class="permalink" href="perldata.html#Scalar-values">#</a><a id="Scalar"></a>Scalar values    </h2>

<p>All data in Perl is a scalar, an array of scalars, or a hash of scalars. A scalar may contain one single value in any of three different flavors: a number, a string, or a reference. In general, conversion from one form to another is transparent. Although a scalar may not directly hold multiple values, it may contain a reference to an array or hash which in turn contains multiple values.</p>

<p>Scalars aren&#39;t necessarily one thing or another. There&#39;s no place to declare a scalar variable to be of type &quot;string&quot;, type &quot;number&quot;, type &quot;reference&quot;, or anything else. Because of the automatic conversion of scalars, operations that return scalars don&#39;t need to care (and in fact, cannot care) whether their caller is looking for a string, a number, or a reference. Perl is a contextually polymorphic language whose scalars can be strings, numbers, or references (which includes objects). Although strings and numbers are considered pretty much the same thing for nearly all purposes, references are strongly-typed, uncastable pointers with builtin reference-counting and destructor invocation.</p>

<p>          A scalar value is interpreted as FALSE in the Boolean sense if it is undefined, the null string or the number 0 (or its string equivalent, &quot;0&quot;), and TRUE if it is anything else. The Boolean context is just a special kind of scalar context where no conversion to a string or a number is ever performed. Negation of a true value by <code>!</code> or <code>not</code> returns a special false value. When evaluated as a string it is treated as <code>&quot;&quot;</code>, but as a number, it is treated as 0. Most Perl operators that return true or false behave this way.</p>

<p>There are actually two varieties of null strings (sometimes referred to as &quot;empty&quot; strings), a defined one and an undefined one. The defined version is just a string of length zero, such as <code>&quot;&quot;</code>. The undefined version is the value that indicates that there is no real value for something, such as when there was an error, or at end of file, or when you refer to an uninitialized variable or element of an array or hash. Although in early versions of Perl, an undefined scalar could become defined when first used in a place expecting a defined value, this no longer happens except for rare cases of autovivification as explained in <a href="perlref.html">perlref</a>. You can use the defined() operator to determine whether a scalar value is defined (this has no meaning on arrays or hashes), and the undef() operator to produce an undefined value.     </p>

<p>To find out whether a given string is a valid non-zero number, it&#39;s sometimes enough to test it against both numeric 0 and also lexical &quot;0&quot; (although this will cause noises if warnings are on). That&#39;s because strings that aren&#39;t numbers count as 0, just as they do in <b>awk</b>:</p>

<pre><code>    if ($str == 0 &amp;&amp; $str ne &quot;0&quot;)  {
	warn &quot;That doesn&#39;t look like a number&quot;;
    }</code></pre>

<p>That method may be best because otherwise you won&#39;t treat IEEE notations like <code>NaN</code> or <code>Infinity</code> properly. At other times, you might prefer to determine whether string data can be used numerically by calling the POSIX::strtod() function or by inspecting your string with a regular expression (as documented in <a href="perlre.html">perlre</a>).</p>

<pre><code>    warn &quot;has nondigits&quot;	if     /\D/;
    warn &quot;not a natural number&quot; unless /^\d+$/;             # rejects -3
    warn &quot;not an integer&quot;       unless /^-?\d+$/;           # rejects +3
    warn &quot;not an integer&quot;       unless /^[+-]?\d+$/;
    warn &quot;not a decimal number&quot; unless /^-?\d+\.?\d*$/;     # rejects .2
    warn &quot;not a decimal number&quot; unless /^-?(?:\d+(?:\.\d*)?|\.\d+)$/;
    warn &quot;not a C float&quot;
	unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;</code></pre>

<p>The length of an array is a scalar value. You may find the length of array @days by evaluating <code>$#days</code>, as in <b>csh</b>. However, this isn&#39;t the length of the array; it&#39;s the subscript of the last element, which is a different value since there is ordinarily a 0th element. Assigning to <code>$#days</code> actually changes the length of the array. Shortening an array this way destroys intervening values. Lengthening an array that was previously shortened does not recover values that were in those elements.  </p>

<p>You can also gain some minuscule measure of efficiency by pre-extending an array that is going to get big. You can also extend an array by assigning to an element that is off the end of the array. You can truncate an array down to nothing by assigning the null list () to it. The following are equivalent:</p>

<pre><code>@whatever = ();
$#whatever = -1;</code></pre>

<p>If you evaluate an array in scalar context, it returns the length of the array. (Note that this is not true of lists, which return the last value, like the C comma operator, nor of built-in functions, which return whatever they feel like returning.) The following is always true: </p>

<pre><code>scalar(@whatever) == $#whatever + 1;</code></pre>

<p>Some programmers choose to use an explicit conversion so as to leave nothing to doubt:</p>

<pre><code>$element_count = scalar(@whatever);</code></pre>

<p>If you evaluate a hash in scalar context, it returns a false value if the hash is empty. If there are any key/value pairs, it returns a true value. A more precise definition is version dependent.</p>

<p>Prior to Perl 5.25 the value returned was a string consisting of the number of used buckets and the number of allocated buckets, separated by a slash. This is pretty much useful only to find out whether Perl&#39;s internal hashing algorithm is performing poorly on your data set. For example, you stick 10,000 things in a hash, but evaluating %HASH in scalar context reveals <code>&quot;1/16&quot;</code>, which means only one out of sixteen buckets has been touched, and presumably contains all 10,000 of your items. This isn&#39;t supposed to happen.</p>

<p>As of Perl 5.25 the return was changed to be the count of keys in the hash. If you need access to the old behavior you can use <code>Hash::Util::bucket_ratio()</code> instead.</p>

<p>If a tied hash is evaluated in scalar context, the <code>SCALAR</code> method is called (with a fallback to <code>FIRSTKEY</code>).   </p>

<p>You can preallocate space for a hash by assigning to the keys() function. This rounds up the allocated buckets to the next power of two:</p>

<pre><code>keys(%users) = 1000;		# allocate 1024 buckets</code></pre>

<h2 id="Scalar-value-constructors"><a class="permalink" href="perldata.html#Scalar-value-constructors">#</a><a id="Scalar1"></a>Scalar value constructors  </h2>

<p>Numeric literals are specified in any of the following floating point or integer formats:</p>

<pre><code class="plaintext">12345
12345.67
.23E-10             # a very small number
3.14_15_92          # a very important number
4_294_967_296       # underscore for legibility
0xff                # hex
0xdead_beef         # more hex
0377                # octal (only numbers, begins with 0)
0o12_345            # alternative octal (introduced in Perl 5.33.5)
0b011011            # binary
0x1.999ap-4         # hexadecimal floating point (the &#39;p&#39; is required)</code></pre>

<p>You are allowed to use underscores (underbars) in numeric literals between digits for legibility (but not multiple underscores in a row: <code>23__500</code> is not legal; <code>23_500</code> is). You could, for example, group binary digits by threes (as for a Unix-style mode argument such as 0b110_100_100) or by fours (to represent nibbles, as in 0b1010_0110) or in other groups. </p>

<p>String literals are usually delimited by either single or double quotes. They work much like quotes in the standard Unix shells: double-quoted string literals are subject to backslash and variable substitution; single-quoted strings are not (except for <code>\&#39;</code> and <code>\\</code>). The usual C-style backslash rules apply for making characters such as newline, tab, etc., as well as some more exotic forms. See <a href="perlop.html#Quote-and-Quote-like-Operators">&quot;Quote and Quote-like Operators&quot; in perlop</a> for a list. </p>

<p>Hexadecimal, octal, or binary, representations in string literals (e.g. &#39;0xff&#39;) are not automatically converted to their integer representation. The hex() and oct() functions make these conversions for you. See <a href="perlfunc.html#hex">&quot;hex&quot; in perlfunc</a> and <a href="perlfunc.html#oct">&quot;oct&quot; in perlfunc</a> for more details.</p>

<p>Hexadecimal floating point can start just like a hexadecimal literal, and it can be followed by an optional fractional hexadecimal part, but it must be followed by <code>p</code>, an optional sign, and a power of two. The format is useful for accurately presenting floating point values, avoiding conversions to or from decimal floating point, and therefore avoiding possible loss in precision. Notice that while most current platforms use the 64-bit IEEE 754 floating point, not all do. Another potential source of (low-order) differences are the floating point rounding modes, which can differ between CPUs, operating systems, and compilers, and which Perl doesn&#39;t control.</p>

<p>You can also embed newlines directly in your strings, i.e., they can end on a different line than they begin. This is nice, but if you forget your trailing quote, the error will not be reported until Perl finds another line containing the quote character, which may be much further on in the script. Variable substitution inside strings is limited to scalar variables, arrays, and array or hash slices. (In other words, names beginning with $ or @, followed by an optional bracketed expression as a subscript.) The following code segment prints out &quot;The price is $100.&quot; </p>

<pre><code>$Price = &#39;$100&#39;;	# not interpolated
print &quot;The price is $Price.\n&quot;;	# interpolated</code></pre>

<p>There is no double interpolation in Perl, so the <code>$100</code> is left as is.</p>

<p>By default floating point numbers substituted inside strings use the dot (&quot;.&quot;) as the decimal separator. If <code>use locale</code> is in effect, and POSIX::setlocale() has been called, the character used for the decimal separator is affected by the LC_NUMERIC locale. See <a href="perllocale.html">perllocale</a> and <a href="POSIX.html">POSIX</a>.</p>

<p>As in some shells, you can enclose the variable name in braces to disambiguate it from following alphanumerics (and underscores). You must also do this when interpolating a variable into a string to separate the variable name from a following double-colon or an apostrophe, since these would be otherwise treated as a package separator: </p>

<pre><code>$who = &quot;Larry&quot;;
print PASSWD &quot;${who}::0:0:Superuser:/:/bin/perl\n&quot;;
print &quot;We use ${who}speak when ${who}&#39;s here.\n&quot;;</code></pre>

<p>Without the braces, Perl would have looked for a $whospeak, a <code>$who::0</code>, and a <code>$who&#39;s</code> variable. The last two would be the $0 and the $s variables in the (presumably) non-existent package <code>who</code>.</p>

<p>In fact, a simple identifier within such curlies is forced to be a string, and likewise within a hash subscript. Neither need quoting. Our earlier example, <code>$days{&#39;Feb&#39;}</code> can be written as <code>$days{Feb}</code> and the quotes will be assumed automatically. But anything more complicated in the subscript will be interpreted as an expression. This means for example that <code>$version{2.0}++</code> is equivalent to <code>$version{2}++</code>, not to <code>$version{&#39;2.0&#39;}++</code>.</p>

<h3 id="Special-floating-point:-infinity-(Inf)-and-not-a-number-(NaN)"><a class="permalink" href="perldata.html#Special-floating-point:-infinity-(Inf)-and-not-a-number-(NaN)">#</a><a id="Special"></a><a id="Special-floating-point:-infinity-Inf-and-not-a-number-NaN"></a>Special floating point: infinity (Inf) and not-a-number (NaN)</h3>

<p>Floating point values include the special values <code>Inf</code> and <code>NaN</code>, for infinity and not-a-number. The infinity can be also negative.</p>

<p>The infinity is the result of certain math operations that overflow the floating point range, like 9**9**9. The not-a-number is the result when the result is undefined or unrepresentable. Though note that you cannot get <code>NaN</code> from some common &quot;undefined&quot; or &quot;out-of-range&quot; operations like dividing by zero, or square root of a negative number, since Perl generates fatal errors for those.</p>

<p>The infinity and not-a-number have their own special arithmetic rules. The general rule is that they are &quot;contagious&quot;: <code>Inf</code> plus one is <code>Inf</code>, and <code>NaN</code> plus one is <code>NaN</code>. Where things get interesting is when you combine infinities and not-a-numbers: <code>Inf</code> minus <code>Inf</code> and <code>Inf</code> divided by <code>Inf</code> are <code>NaN</code> (while <code>Inf</code> plus <code>Inf</code> is <code>Inf</code> and <code>Inf</code> times <code>Inf</code> is <code>Inf</code>). <code>NaN</code> is also curious in that it does not equal any number, <i>including</i> itself: <code>NaN</code> != <code>NaN</code>.</p>

<p>Perl doesn&#39;t understand <code>Inf</code> and <code>NaN</code> as numeric literals, but you can have them as strings, and Perl will convert them as needed: &quot;Inf&quot; + 1. (You can, however, import them from the POSIX extension; <code>use POSIX qw(Inf NaN);</code> and then use them as literals.)</p>

<p>Note that on input (string to number) Perl accepts <code>Inf</code> and <code>NaN</code> in many forms. Case is ignored, and the Win32-specific forms like <code>1.#INF</code> are understood, but on output the values are normalized to <code>Inf</code> and <code>NaN</code>.</p>

<h3 id="Version-Strings"><a class="permalink" href="perldata.html#Version-Strings">#</a><a id="Version"></a>Version Strings   </h3>

<p>A literal of the form <code>v1.20.300.4000</code> is parsed as a string composed of characters with the specified ordinals. This form, known as v-strings, provides an alternative, more readable way to construct strings, rather than use the somewhat less readable interpolation form <code>&quot;\x{1}\x{14}\x{12c}\x{fa0}&quot;</code>. This is useful for representing Unicode strings, and for comparing version &quot;numbers&quot; using the string comparison operators, <code>cmp</code>, <code>gt</code>, <code>lt</code> etc. If there are two or more dots in the literal, the leading <code>v</code> may be omitted.</p>

<pre><code>print v9786;              # prints SMILEY, &quot;\x{263a}&quot;
print v102.111.111;       # prints &quot;foo&quot;
print 102.111.111;        # same</code></pre>

<p>Such literals are accepted by both <code>require</code> and <code>use</code> for doing a version check. Note that using the v-strings for IPv4 addresses is not portable unless you also use the inet_aton()/inet_ntoa() routines of the Socket package.</p>

<p>Note that since Perl 5.8.1 the single-number v-strings (like <code>v65</code>) are not v-strings before the <code>=&gt;</code> operator (which is usually used to separate a hash key from a hash value); instead they are interpreted as literal strings (&#39;v65&#39;). They were v-strings from Perl 5.6.0 to Perl 5.8.0, but that caused more confusion and breakage than good. Multi-number v-strings like <code>v65.66</code> and <code>65.66.67</code> continue to be v-strings always.</p>

<h3 id="Special-Literals"><a class="permalink" href="perldata.html#Special-Literals">#</a><a id="Special1"></a>Special Literals         </h3>

<p>The special literals __FILE__, __LINE__, and __PACKAGE__ represent the current filename, line number, and package name at that point in your program. __SUB__ gives a reference to the current subroutine. They may be used only as separate tokens; they will not be interpolated into strings. If there is no current package (due to an empty <code>package;</code> directive), __PACKAGE__ is the undefined value. (But the empty <code>package;</code> is no longer supported, as of version 5.10.) Outside of a subroutine, __SUB__ is the undefined value. __SUB__ is only available in 5.16 or higher, and only with a <code>use v5.16</code> or <code>use feature &quot;current_sub&quot;</code> declaration.       </p>

<p>The two control characters ^D and ^Z, and the tokens __END__ and __DATA__ may be used to indicate the logical end of the script before the actual end of file. Any following text is ignored by the interpreter unless read by the program as described below.</p>

<p>Text after __DATA__ may be read via the filehandle <code>PACKNAME::DATA</code>, where <code>PACKNAME</code> is the package that was current when the __DATA__ token was encountered. The filehandle is left open pointing to the line after __DATA__. The program should <code>close DATA</code> when it is done reading from it. (Leaving it open leaks filehandles if the module is reloaded for any reason, so it&#39;s a safer practice to close it.) For compatibility with older scripts written before __DATA__ was introduced, __END__ behaves like __DATA__ in the top level script (but not in files loaded with <code>require</code> or <code>do</code>) and leaves the remaining contents of the file accessible via <code>main::DATA</code>.</p>

<pre><code>while (my $line = &lt;DATA&gt;) { print $line; }
close DATA;
__DATA__
Hello world.</code></pre>

<p>The <code>DATA</code> file handle by default has whatever PerlIO layers were in place when Perl read the file to parse the source. Normally that means that the file is being read bytewise, as if it were encoded in Latin-1, but there are two major ways for it to be otherwise. Firstly, if the <code>__END__</code>/<code>__DATA__</code> token is in the scope of a <code>use utf8</code> pragma then the <code>DATA</code> handle will be in UTF-8 mode. And secondly, if the source is being read from perl&#39;s standard input then the <code>DATA</code> file handle is actually aliased to the <code>STDIN</code> file handle, and may be in UTF-8 mode because of the <code>PERL_UNICODE</code> environment variable or perl&#39;s command-line switches.</p>

<p>See <a href="SelfLoader.html">SelfLoader</a> for more description of __DATA__, and an example of its use. Note that you cannot read from the DATA filehandle in a BEGIN block: the BEGIN block is executed as soon as it is seen (during compilation), at which point the corresponding __DATA__ (or __END__) token has not yet been seen.</p>

<h3 id="Barewords"><a class="permalink" href="perldata.html#Barewords">#</a>Barewords </h3>

<p>A word that has no other interpretation in the grammar will be treated as if it were a quoted string. These are known as &quot;barewords&quot;. As with filehandles and labels, a bareword that consists entirely of lowercase letters risks conflict with future reserved words, and if you use the <code>use warnings</code> pragma or the <b>-w</b> switch, Perl will warn you about any such words. Perl limits barewords (like identifiers) to about 250 characters. Future versions of Perl are likely to eliminate these arbitrary limitations.</p>

<p>Some people may wish to outlaw barewords entirely. If you say</p>

<pre><code>use strict &#39;subs&#39;;</code></pre>

<p>then any bareword that would NOT be interpreted as a subroutine call produces a compile-time error instead. The restriction lasts to the end of the enclosing block. An inner block may countermand this by saying <code>no strict &#39;subs&#39;</code>.</p>

<h3 id="Array-Interpolation"><a class="permalink" href="perldata.html#Array-Interpolation">#</a><a id="Array"></a>Array Interpolation   </h3>

<p>Arrays and slices are interpolated into double-quoted strings by joining the elements with the delimiter specified in the <code>$&quot;</code> variable (<code>$LIST_SEPARATOR</code> if &quot;use English;&quot; is specified), space by default. The following are equivalent:</p>

<pre><code>$temp = join($&quot;, @ARGV);
system &quot;echo $temp&quot;;

system &quot;echo @ARGV&quot;;</code></pre>

<p>Within search patterns (which also undergo double-quotish substitution) there is an unfortunate ambiguity: Is <code>/$foo[bar]/</code> to be interpreted as <code>/${foo}[bar]/</code> (where <code>[bar]</code> is a character class for the regular expression) or as <code>/${foo[bar]}/</code> (where <code>[bar]</code> is the subscript to array @foo)? If @foo doesn&#39;t otherwise exist, then it&#39;s obviously a character class. If @foo exists, Perl takes a good guess about <code>[bar]</code>, and is almost always right. If it does guess wrong, or if you&#39;re just plain paranoid, you can force the correct interpretation with curly braces as above.</p>

<p>If you&#39;re looking for the information on how to use here-documents, which used to be here, that&#39;s been moved to <a href="perlop.html#Quote-and-Quote-like-Operators">&quot;Quote and Quote-like Operators&quot; in perlop</a>.</p>

<h2 id="List-value-constructors"><a class="permalink" href="perldata.html#List-value-constructors">#</a><a id="List"></a>List value constructors </h2>

<p>List values are denoted by separating individual values by commas (and enclosing the list in parentheses where precedence requires it):</p>

<pre><code class="plaintext">(LIST)</code></pre>

<p>In a context not requiring a list value, the value of what appears to be a list literal is simply the value of the final element, as with the C comma operator. For example,</p>

<pre><code>@foo = (&#39;cc&#39;, &#39;-E&#39;, $bar);</code></pre>

<p>assigns the entire list value to array @foo, but</p>

<pre><code>$foo = (&#39;cc&#39;, &#39;-E&#39;, $bar);</code></pre>

<p>assigns the value of variable $bar to the scalar variable $foo. Note that the value of an actual array in scalar context is the length of the array; the following assigns the value 3 to $foo:</p>

<pre><code>@foo = (&#39;cc&#39;, &#39;-E&#39;, $bar);
$foo = @foo;                # $foo gets 3</code></pre>

<p>You may have an optional comma before the closing parenthesis of a list literal, so that you can say:</p>

<pre><code>@foo = (
    1,
    2,
    3,
);</code></pre>

<p>To use a here-document to assign an array, one line per element, you might use an approach like this:</p>

<pre><code>@sauces = &lt;&lt;End_Lines =~ m/(\S.*\S)/g;
    normal tomato
    spicy tomato
    green chile
    pesto
    white wine
End_Lines</code></pre>

<p>LISTs do automatic interpolation of sublists. That is, when a LIST is evaluated, each element of the list is evaluated in list context, and the resulting list value is interpolated into LIST just as if each individual element were a member of LIST. Thus arrays and hashes lose their identity in a LIST--the list</p>

<pre><code>(@foo,@bar,&amp;SomeSub,%glarch)</code></pre>

<p>contains all the elements of @foo followed by all the elements of @bar, followed by all the elements returned by the subroutine named SomeSub called in list context, followed by the key/value pairs of %glarch. To make a list reference that does <i>NOT</i> interpolate, see <a href="perlref.html">perlref</a>.</p>

<p>The null list is represented by (). Interpolating it in a list has no effect. Thus ((),(),()) is equivalent to (). Similarly, interpolating an array with no elements is the same as if no array had been interpolated at that point.</p>

<p>This interpolation combines with the facts that the opening and closing parentheses are optional (except when necessary for precedence) and lists may end with an optional comma to mean that multiple commas within lists are legal syntax. The list <code>1,,3</code> is a concatenation of two lists, <code>1,</code> and <code>3</code>, the first of which ends with that optional comma. <code>1,,3</code> is <code>(1,),(3)</code> is <code>1,3</code> (And similarly for <code>1,,,3</code> is <code>(1,),(,),3</code> is <code>1,3</code> and so on.) Not that we&#39;d advise you to use this obfuscation.</p>

<p>A list value may also be subscripted like a normal array. You must put the list in parentheses to avoid ambiguity. For example:</p>

<pre><code># Stat returns list value.
$time = (stat($file))[8];

# SYNTAX ERROR HERE.
$time = stat($file)[8];  # OOPS, FORGOT PARENTHESES

# Find a hex digit.
$hexdigit = (&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;f&#39;)[$digit-10];

# A &quot;reverse comma operator&quot;.
return (pop(@foo),pop(@foo))[0];</code></pre>

<p>Lists may be assigned to only when each element of the list is itself legal to assign to:</p>

<pre><code>($x, $y, $z) = (1, 2, 3);

($map{&#39;red&#39;}, $map{&#39;blue&#39;}, $map{&#39;green&#39;}) = (0x00f, 0x0f0, 0xf00);</code></pre>

<p>An exception to this is that you may assign to <code>undef</code> in a list. This is useful for throwing away some of the return values of a function:</p>

<pre><code>($dev, $ino, undef, undef, $uid, $gid) = stat($file);</code></pre>

<p>As of Perl 5.22, you can also use <code>(undef)x2</code> instead of <code>undef, undef</code>. (You can also do <code>($x) x 2</code>, which is less useful, because it assigns to the same variable twice, clobbering the first value assigned.)</p>

<p>When you assign a list of scalars to an array, all previous values in that array are wiped out and the number of elements in the array will now be equal to the number of elements in the right-hand list -- the list from which assignment was made. The array will automatically resize itself to precisely accommodate each element in the right-hand list.</p>

<pre><code>use warnings;
my (@xyz, $x, $y, $z);

@xyz = (1, 2, 3);
print &quot;@xyz\n&quot;;                             # 1 2 3

@xyz = (&#39;al&#39;, &#39;be&#39;, &#39;ga&#39;, &#39;de&#39;);
print &quot;@xyz\n&quot;;                             # al be ga de

@xyz = (101, 102);
print &quot;@xyz\n&quot;;                             # 101 102</code></pre>

<p>When, however, you assign a list of scalars to another list of scalars, the results differ according to whether the left-hand list -- the list being assigned to -- has the same, more or fewer elements than the right-hand list.</p>

<pre><code>($x, $y, $z) = (1, 2, 3);
print &quot;$x $y $z\n&quot;;                         # 1 2 3

($x, $y, $z) = (&#39;al&#39;, &#39;be&#39;, &#39;ga&#39;, &#39;de&#39;);
print &quot;$x $y $z\n&quot;;                         # al be ga

($x, $y, $z) = (101, 102);
print &quot;$x $y $z\n&quot;;                         # 101 102
# Use of uninitialized value $z in concatenation (.)
# or string at [program] line [line number].</code></pre>

<p>If the number of scalars in the left-hand list is less than that in the right-hand list, the &quot;extra&quot; scalars in the right-hand list will simply not be assigned.</p>

<p>If the number of scalars in the left-hand list is greater than that in the left-hand list, the &quot;missing&quot; scalars will become undefined.</p>

<pre><code>($x, $y, $z) = (101, 102);
for my $el ($x, $y, $z) {
    (defined $el) ? print &quot;$el &quot; : print &quot;&lt;undef&gt;&quot;;
}
print &quot;\n&quot;;
                                            # 101 102 &lt;undef&gt;</code></pre>

<p>List assignment in scalar context returns the number of elements produced by the expression on the right side of the assignment:</p>

<pre><code>$x = (($foo,$bar) = (3,2,1));       # set $x to 3, not 2
$x = (($foo,$bar) = f());           # set $x to f()&#39;s return count</code></pre>

<p>This is handy when you want to do a list assignment in a Boolean context, because most list functions return a null list when finished, which when assigned produces a 0, which is interpreted as FALSE.</p>

<p>It&#39;s also the source of a useful idiom for executing a function or performing an operation in list context and then counting the number of return values, by assigning to an empty list and then using that assignment in scalar context. For example, this code:</p>

<pre><code>$count = () = $string =~ /\d+/g;</code></pre>

<p>will place into $count the number of digit groups found in $string. This happens because the pattern match is in list context (since it is being assigned to the empty list), and will therefore return a list of all matching parts of the string. The list assignment in scalar context will translate that into the number of elements (here, the number of times the pattern matched) and assign that to $count. Note that simply using</p>

<pre><code>$count = $string =~ /\d+/g;</code></pre>

<p>would not have worked, since a pattern match in scalar context will only return true or false, rather than a count of matches.</p>

<p>The final element of a list assignment may be an array or a hash:</p>

<pre><code>($x, $y, @rest) = split;
my($x, $y, %rest) = @_;</code></pre>

<p>You can actually put an array or hash anywhere in the list, but the first one in the list will soak up all the values, and anything after it will become undefined. This may be useful in a my() or local().</p>

<p>A hash can be initialized using a literal list holding pairs of items to be interpreted as a key and a value:</p>

<pre><code># same as map assignment above
%map = (&#39;red&#39;,0x00f,&#39;blue&#39;,0x0f0,&#39;green&#39;,0xf00);</code></pre>

<p>While literal lists and named arrays are often interchangeable, that&#39;s not the case for hashes. Just because you can subscript a list value like a normal array does not mean that you can subscript a list value as a hash. Likewise, hashes included as parts of other lists (including parameters lists and return lists from functions) always flatten out into key/value pairs. That&#39;s why it&#39;s good to use references sometimes.</p>

<p>It is often more readable to use the <code>=&gt;</code> operator between key/value pairs. The <code>=&gt;</code> operator is mostly just a more visually distinctive synonym for a comma, but it also arranges for its left-hand operand to be interpreted as a string if it&#39;s a bareword that would be a legal simple identifier. <code>=&gt;</code> doesn&#39;t quote compound identifiers, that contain double colons. This makes it nice for initializing hashes:</p>

<pre><code> %map = (
              red   =&gt; 0x00f,
              blue  =&gt; 0x0f0,
              green =&gt; 0xf00,
);</code></pre>

<p>or for initializing hash references to be used as records:</p>

<pre><code>$rec = {
            witch =&gt; &#39;Mable the Merciless&#39;,
            cat   =&gt; &#39;Fluffy the Ferocious&#39;,
            date  =&gt; &#39;10/31/1776&#39;,
};</code></pre>

<p>or for using call-by-named-parameter to complicated functions:</p>

<pre><code>$field = $query-&gt;radio_group(
            name      =&gt; &#39;group_name&#39;,
            values    =&gt; [&#39;eenie&#39;,&#39;meenie&#39;,&#39;minie&#39;],
            default   =&gt; &#39;meenie&#39;,
            linebreak =&gt; &#39;true&#39;,
            labels    =&gt; \%labels
);</code></pre>

<p>Note that just because a hash is initialized in that order doesn&#39;t mean that it comes out in that order. See <a href="perlfunc.html#sort">&quot;sort&quot; in perlfunc</a> for examples of how to arrange for an output ordering.</p>

<p>If a key appears more than once in the initializer list of a hash, the last occurrence wins:</p>

<pre><code>%circle = (
              center =&gt; [5, 10],
              center =&gt; [27, 9],
              radius =&gt; 100,
              color =&gt; [0xDF, 0xFF, 0x00],
              radius =&gt; 54,
);

# same as
%circle = (
              center =&gt; [27, 9],
              color =&gt; [0xDF, 0xFF, 0x00],
              radius =&gt; 54,
);</code></pre>

<p>This can be used to provide overridable configuration defaults:</p>

<pre><code># values in %args take priority over %config_defaults
%config = (%config_defaults, %args);</code></pre>

<h2 id="Subscripts"><a class="permalink" href="perldata.html#Subscripts">#</a>Subscripts</h2>

<p>An array can be accessed one scalar at a time by specifying a dollar sign (<code>$</code>), then the name of the array (without the leading <code>@</code>), then the subscript inside square brackets. For example:</p>

<pre><code>@myarray = (5, 50, 500, 5000);
print &quot;The Third Element is&quot;, $myarray[2], &quot;\n&quot;;</code></pre>

<p>The array indices start with 0. A negative subscript retrieves its value from the end. In our example, <code>$myarray[-1]</code> would have been 5000, and <code>$myarray[-2]</code> would have been 500.</p>

<p>Hash subscripts are similar, only instead of square brackets curly brackets are used. For example:</p>

<pre><code>%scientists = 
(
    &quot;Newton&quot; =&gt; &quot;Isaac&quot;,
    &quot;Einstein&quot; =&gt; &quot;Albert&quot;,
    &quot;Darwin&quot; =&gt; &quot;Charles&quot;,
    &quot;Feynman&quot; =&gt; &quot;Richard&quot;,
);

print &quot;Darwin&#39;s First Name is &quot;, $scientists{&quot;Darwin&quot;}, &quot;\n&quot;;</code></pre>

<p>You can also subscript a list to get a single element from it:</p>

<pre><code>$dir = (getpwnam(&quot;daemon&quot;))[7];</code></pre>

<h2 id="Multi-dimensional-array-emulation"><a class="permalink" href="perldata.html#Multi-dimensional-array-emulation">#</a><a id="Multi"></a>Multi-dimensional array emulation</h2>

<p>Multidimensional arrays may be emulated by subscripting a hash with a list. The elements of the list are joined with the subscript separator (see <a href="perlvar.html#%24%3B">&quot;$;&quot; in perlvar</a>).</p>

<pre><code>$foo{$x,$y,$z}</code></pre>

<p>is equivalent to</p>

<pre><code>$foo{join($;, $x, $y, $z)}</code></pre>

<p>The default subscript separator is &quot;\034&quot;, the same as SUBSEP in <b>awk</b>.</p>

<h2 id="Slices"><a class="permalink" href="perldata.html#Slices">#</a>Slices   </h2>

<p>A slice accesses several elements of a list, an array, or a hash simultaneously using a list of subscripts. It&#39;s more convenient than writing out the individual elements as a list of separate scalar values.</p>

<pre><code>($him, $her)   = @folks[0,-1];              # array slice
@them          = @folks[0 .. 3];            # array slice
($who, $home)  = @ENV{&quot;USER&quot;, &quot;HOME&quot;};      # hash slice
($uid, $dir)   = (getpwnam(&quot;daemon&quot;))[2,7]; # list slice</code></pre>

<p>Since you can assign to a list of variables, you can also assign to an array or hash slice.</p>

<pre><code>@days[3..5]    = qw/Wed Thu Fri/;
@colors{&#39;red&#39;,&#39;blue&#39;,&#39;green&#39;} 
               = (0xff0000, 0x0000ff, 0x00ff00);
@folks[0, -1]  = @folks[-1, 0];</code></pre>

<p>The previous assignments are exactly equivalent to</p>

<pre><code>($days[3], $days[4], $days[5]) = qw/Wed Thu Fri/;
($colors{&#39;red&#39;}, $colors{&#39;blue&#39;}, $colors{&#39;green&#39;})
               = (0xff0000, 0x0000ff, 0x00ff00);
($folks[0], $folks[-1]) = ($folks[-1], $folks[0]);</code></pre>

<p>Since changing a slice changes the original array or hash that it&#39;s slicing, a <code>foreach</code> construct will alter some--or even all--of the values of the array or hash.</p>

<pre><code>foreach (@array[ 4 .. 10 ]) { s/peter/paul/ } 

foreach (@hash{qw[key1 key2]}) {
    s/^\s+//;           # trim leading whitespace
    s/\s+$//;           # trim trailing whitespace
    s/(\w+)/\u\L$1/g;   # &quot;titlecase&quot; words
}</code></pre>

<p>As a special exception, when you slice a list (but not an array or a hash), if the list evaluates to empty, then taking a slice of that empty list will always yield the empty list in turn. Thus:</p>

<pre><code>@a = ()[0,1];          # @a has no elements
@b = (@a)[0,1];        # @b has no elements
@c = (sub{}-&gt;())[0,1]; # @c has no elements
@d = (&#39;a&#39;,&#39;b&#39;)[0,1];   # @d has two elements
@e = (@d)[0,1,8,9];    # @e has four elements
@f = (@d)[8,9];        # @f has two elements</code></pre>

<p>This makes it easy to write loops that terminate when a null list is returned:</p>

<pre><code>while ( ($home, $user) = (getpwent)[7,0] ) {
    printf &quot;%-8s %s\n&quot;, $user, $home;
}</code></pre>

<p>As noted earlier in this document, the scalar sense of list assignment is the number of elements on the right-hand side of the assignment. The null list contains no elements, so when the password file is exhausted, the result is 0, not 2.</p>

<p>Slices in scalar context return the last item of the slice.</p>

<pre><code>@a = qw/first second third/;
%h = (first =&gt; &#39;A&#39;, second =&gt; &#39;B&#39;);
$t = @a[0, 1];                  # $t is now &#39;second&#39;
$u = @h{&#39;first&#39;, &#39;second&#39;};     # $u is now &#39;B&#39;</code></pre>

<p>If you&#39;re confused about why you use an &#39;@&#39; there on a hash slice instead of a &#39;%&#39;, think of it like this. The type of bracket (square or curly) governs whether it&#39;s an array or a hash being looked at. On the other hand, the leading symbol (&#39;$&#39; or &#39;@&#39;) on the array or hash indicates whether you are getting back a singular value (a scalar) or a plural one (a list).</p>

<h3 id="Key/Value-Hash-Slices"><a class="permalink" href="perldata.html#Key/Value-Hash-Slices">#</a><a id="Key"></a><a id="Key-Value-Hash-Slices"></a>Key/Value Hash Slices</h3>

<p>Starting in Perl 5.20, a hash slice operation with the % symbol is a variant of slice operation returning a list of key/value pairs rather than just values:</p>

<pre><code>%h = (blonk =&gt; 2, foo =&gt; 3, squink =&gt; 5, bar =&gt; 8);
%subset = %h{&#39;foo&#39;, &#39;bar&#39;}; # key/value hash slice
# %subset is now (foo =&gt; 3, bar =&gt; 8)
%removed = delete %h{&#39;foo&#39;, &#39;bar&#39;};
# %removed is now (foo =&gt; 3, bar =&gt; 8)
# %h is now (blonk =&gt; 2, squink =&gt; 5)</code></pre>

<p>However, the result of such a slice cannot be localized or assigned to. These are otherwise very much consistent with hash slices using the @ symbol.</p>

<h3 id="Index/Value-Array-Slices"><a class="permalink" href="perldata.html#Index/Value-Array-Slices">#</a><a id="Index"></a><a id="Index-Value-Array-Slices"></a>Index/Value Array Slices</h3>

<p>Similar to key/value hash slices (and also introduced in Perl 5.20), the % array slice syntax returns a list of index/value pairs:</p>

<pre><code>@a = &quot;a&quot;..&quot;z&quot;;
@list = %a[3,4,6];
# @list is now (3, &quot;d&quot;, 4, &quot;e&quot;, 6, &quot;g&quot;)
@removed = delete %a[3,4,6]
# @removed is now (3, &quot;d&quot;, 4, &quot;e&quot;, 6, &quot;g&quot;)
# @list[3,4,6] are now undef</code></pre>

<p>Note that calling <a href="perlfunc.html#delete-EXPR"><code>delete</code></a> on array values is strongly discouraged.</p>

<h2 id="Typeglobs-and-Filehandles"><a class="permalink" href="perldata.html#Typeglobs-and-Filehandles">#</a><a id="Typeglobs"></a>Typeglobs and Filehandles   </h2>

<p>Perl uses an internal type called a <i>typeglob</i> to hold an entire symbol table entry. The type prefix of a typeglob is a <code>*</code>, because it represents all types. This used to be the preferred way to pass arrays and hashes by reference into a function, but now that we have real references, this is seldom needed.</p>

<p>The main use of typeglobs in modern Perl is create symbol table aliases. This assignment:</p>

<pre><code>*this = *that;</code></pre>

<p>makes $this an alias for $that, @this an alias for @that, %this an alias for %that, &amp;this an alias for &amp;that, etc. Much safer is to use a reference. This:</p>

<pre><code>local *Here::blue = \$There::green;</code></pre>

<p>temporarily makes $Here::blue an alias for $There::green, but doesn&#39;t make @Here::blue an alias for @There::green, or %Here::blue an alias for %There::green, etc. See <a href="perlmod.html#Symbol-Tables">&quot;Symbol Tables&quot; in perlmod</a> for more examples of this. Strange though this may seem, this is the basis for the whole module import/export system.</p>

<p>Another use for typeglobs is to pass filehandles into a function or to create new filehandles. If you need to use a typeglob to save away a filehandle, do it this way:</p>

<pre><code>$fh = *STDOUT;</code></pre>

<p>or perhaps as a real reference, like this:</p>

<pre><code>$fh = \*STDOUT;</code></pre>

<p>See <a href="perlsub.html">perlsub</a> for examples of using these as indirect filehandles in functions.</p>

<p>Typeglobs are also a way to create a local filehandle using the local() operator. These last until their block is exited, but may be passed back. For example:</p>

<pre><code>sub newopen {
    my $path = shift;
    local  *FH;  # not my!
    open   (FH, $path)          or  return undef;
    return *FH;
}
$fh = newopen(&#39;/etc/passwd&#39;);</code></pre>

<p>Now that we have the <code>*foo{THING}</code> notation, typeglobs aren&#39;t used as much for filehandle manipulations, although they&#39;re still needed to pass brand new file and directory handles into or out of functions. That&#39;s because <code>*HANDLE{IO}</code> only works if HANDLE has already been used as a handle. In other words, <code>*FH</code> must be used to create new symbol table entries; <code>*foo{THING}</code> cannot. When in doubt, use <code>*FH</code>.</p>

<p>All functions that are capable of creating filehandles (open(), opendir(), pipe(), socketpair(), sysopen(), socket(), and accept()) automatically create an anonymous filehandle if the handle passed to them is an uninitialized scalar variable. This allows the constructs such as <code>open(my $fh, ...)</code> and <code>open(local $fh,...)</code> to be used to create filehandles that will conveniently be closed automatically when the scope ends, provided there are no other references to them. This largely eliminates the need for typeglobs when opening filehandles that must be passed around, as in the following example:</p>

<pre><code>sub myopen {
    open my $fh, &quot;@_&quot;
         or die &quot;Can&#39;t open &#39;@_&#39;: $!&quot;;
    return $fh;
}

{
    my $f = myopen(&quot;&lt;/etc/motd&quot;);
    print &lt;$f&gt;;
    # $f implicitly closed here
}</code></pre>

<p>Note that if an initialized scalar variable is used instead the result is different: <code>my $fh=&#39;zzz&#39;; open($fh, ...)</code> is equivalent to <code>open( *{&#39;zzz&#39;}, ...)</code>. <code>use strict &#39;refs&#39;</code> forbids such practice.</p>

<p>Another way to create anonymous filehandles is with the Symbol module or with the IO::Handle module and its ilk. These modules have the advantage of not hiding different types of the same name during the local(). See the bottom of <a href="perlfunc.html#open">&quot;open&quot; in perlfunc</a> for an example.</p>

<h1 id="SEE-ALSO"><a class="permalink" href="perldata.html#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1>

<p>See <a href="perlvar.html">perlvar</a> for a description of Perl&#39;s built-in variables and a discussion of legal variable names. See <a href="perlref.html">perlref</a>, <a href="perlsub.html">perlsub</a>, and <a href="perlmod.html#Symbol-Tables">&quot;Symbol Tables&quot; in perlmod</a> for more discussion on typeglobs and the <code>*foo{THING}</code> syntax.</p>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
